package com.byxy.shop.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.byxy.shop.dao.CarouselDao;
import com.byxy.shop.dao.GoodsCategoryDao;
import com.byxy.shop.dao.GoodsInfoDao;
import com.byxy.shop.dao.IndexConfigDao;
import com.byxy.shop.entity.Carousel;
import com.byxy.shop.entity.GoodsCategory;
import com.byxy.shop.entity.GoodsInfo;
import com.byxy.shop.entity.IndexConfig;
import com.byxy.shop.service.MallIndexService;
import com.byxy.ssm.shop.vo.CarouselVO;
import com.byxy.ssm.shop.vo.CategoryVO;
import com.byxy.ssm.shop.vo.HotGoodsVO;
import com.byxy.ssm.shop.vo.NewGoodsVO;
import com.byxy.ssm.shop.vo.RecommendGoodsVO;
import com.github.pagehelper.PageHelper;

/**
 * @author 廖善椅
 * @ClassName MallIndexServiceImpl
 * @Description 业务接口实现类
 * @date 2020年5月8日
 */

@Service
@Transactional
public class MallIndexServiceImpl implements MallIndexService {

	@Autowired
	private CarouselDao carouselDao;
	@Autowired
	private GoodsCategoryDao goodsCategoryDao;
	@Autowired
	private GoodsInfoDao goodsInfoDao;
	@Autowired
	private IndexConfigDao indexConfigDao;

	@Override
	public List<CategoryVO> findCategoryVO() {
		// 对一级菜单进行降序排列
		PageHelper.orderBy("category_rank DESC");
		List<CategoryVO> categoryVOs = new ArrayList<>();
		List<GoodsCategory> goodsCategorys = goodsCategoryDao.findCategoryByLevel(1);
		for (GoodsCategory goodsCategory : goodsCategorys) {
			CategoryVO categoryVO = new CategoryVO();
			BeanUtils.copyProperties(goodsCategory, categoryVO);
			// Son
			List<CategoryVO> sonslist = new ArrayList<>();
			List<GoodsCategory> sons = goodsCategoryDao.findCategoryByLevelAndId(2, goodsCategory.getCategoryId());
			for (GoodsCategory categoryson : sons) {
				CategoryVO son = new CategoryVO();
				BeanUtils.copyProperties(categoryson, son);
				// grandson
				List<CategoryVO> grandsonlist = new ArrayList<>();
				List<GoodsCategory> grandsons = goodsCategoryDao.findCategoryByLevelAndId(3, categoryson.getCategoryId());
				for (GoodsCategory grandson : grandsons) {
					CategoryVO grandchild = new CategoryVO();
					BeanUtils.copyProperties(grandson, grandchild);
					grandsonlist.add(grandchild);
				}
				son.setCategorySon(grandsonlist);
				sonslist.add(son);
			}
			categoryVO.setCategorySon(sonslist);
			// 比较大小是否大于0
			if (sonslist.size() > 0) {
				categoryVOs.add(categoryVO);
			}
		}
		return categoryVOs;
	}

	@Override
	public List<HotGoodsVO> findHotGoodsVO() {
		// 查条4条config_type=3的热销商品 并按照config_rank进行降序排列
		PageHelper.startPage(1, 4);
		PageHelper.orderBy("config_rank DESC");
		List<IndexConfig> configs = indexConfigDao.findGoods(3, 0);
		List<HotGoodsVO> hotGoodsVOs = new ArrayList<>();
		for (IndexConfig indexConfig : configs) {
			GoodsInfo info = goodsInfoDao.getById(indexConfig.getGoodsId());
			HotGoodsVO hotGoodsVO = new HotGoodsVO();
			BeanUtils.copyProperties(info, hotGoodsVO);
			hotGoodsVOs.add(hotGoodsVO);
		}
		return hotGoodsVOs;
	}

	@Override
	public List<NewGoodsVO> findNewGoodsVO() {
		// 查条5条config_type=4的新品 并按照config_rank进行降序排列
		PageHelper.startPage(1, 5);
		PageHelper.orderBy("config_rank DESC");
		List<IndexConfig> configs = indexConfigDao.findGoods(4, 0);
		List<NewGoodsVO> newGoodsVOs = new ArrayList<>();
		for (IndexConfig indexConfig : configs) {
			GoodsInfo info = goodsInfoDao.getById(indexConfig.getGoodsId());
			NewGoodsVO newGoodsVO = new NewGoodsVO();
			BeanUtils.copyProperties(info, newGoodsVO);
			newGoodsVOs.add(newGoodsVO);
		}
		return newGoodsVOs;
	}

	@Override
	public List<RecommendGoodsVO> findRecommendGoodsVO() {
		List<GoodsInfo> iList = new ArrayList<>();
		// 查条5条config_type=4的新品 并按照config_rank进行降序排列
		PageHelper.startPage(1, 10);
		PageHelper.orderBy("config_rank DESC");
		List<IndexConfig> configs = indexConfigDao.findGoods(5, 0);
		List<RecommendGoodsVO> rGoodsVOs = new ArrayList<>();
		for (IndexConfig indexConfig : configs) {
			PageHelper.orderBy("stock_num ASC");
			GoodsInfo info = goodsInfoDao.getById(indexConfig.getGoodsId());
			iList.add(info);
			RecommendGoodsVO rGoodsVO = new RecommendGoodsVO();
			BeanUtils.copyProperties(info, rGoodsVO);
			rGoodsVOs.add(rGoodsVO);
		}
		return rGoodsVOs;
	}

	@Override
	public List<CarouselVO> findCarouselVO() {
		List<Carousel> carousels = carouselDao.findAllCarousel();
		List<CarouselVO> carouselVOs = new ArrayList<>();
		for (Carousel carousel : carousels) {
			CarouselVO carouselVO = new CarouselVO();
			BeanUtils.copyProperties(carousel, carouselVO);
			carouselVOs.add(carouselVO);
		}
		return carouselVOs;
	}

}
